package com.ruoyi.system.service.impl;

import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.ProductType;
import com.ruoyi.system.domain.vo.eCatalog.ECatalogInfoListVo;
import com.ruoyi.system.domain.vo.eCatalog.ECatalogInfoVo;
import com.ruoyi.system.domain.vo.eCatalog.ECatalogVo;
import com.ruoyi.system.domain.vo.productInfo.ProductDictVo;
import com.ruoyi.system.service.ICarInfoService;
import com.ruoyi.system.service.IProductCarService;
import com.ruoyi.system.service.IProductTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.ProductInfoMapper;
import com.ruoyi.system.domain.ProductInfo;
import com.ruoyi.system.service.IProductInfoService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 产品信息Service业务层处理
 *
 * @author ruoyi
 * @date 2023-01-25
 */
@Service
public class ProductInfoServiceImpl extends ServiceImpl<ProductInfoMapper, ProductInfo> implements IProductInfoService
{
    @Autowired
    private ProductInfoMapper productInfoMapper;

    @Autowired
    private IProductTypeService productTypeService;

    @Autowired
    private ICarInfoService carInfoService;

    @Autowired
    private IProductCarService productCarService;

    /**
     * 查询产品信息
     *
     * @param id 产品信息主键
     * @return 产品信息
     */
    @Override
    public ProductInfo selectProductInfoById(Long id)
    {
        return productInfoMapper.selectProductInfoById(id);
    }

//    /**
//     * 查询产品信息列表,vo转换
//     *
//     * @return 产品信息
//     */
//    @Override
//    public List<ProductInfoListVo> convertProductInfoListVo(List<ProductInfo> infoList)
//    {
////        List<ProductInfo> infoList = productInfoMapper.selectProductInfoList(productInfo);
//        return infoList.stream().map((lis)->{
//            ProductInfoListVo ListVo = new ProductInfoListVo();
//            BeanUtils.copyProperties(lis,ListVo);
//            return ListVo;
//        }).collect(Collectors.toList());
//    }
    /**
     * 查询产品信息列表
     *
     * @param productInfo 产品信息
     * @return 产品信息
     */
    @Override
    public List<ProductInfo> selectProductInfoList(ProductInfo productInfo)
    {
        return productInfoMapper.selectProductInfoList(productInfo);
    }

    @Override
    public List<ProductDictVo> selectProductDict(ProductInfo productInfo) {
//        QueryWrapper<ProductInfo> queryWrapper = new QueryWrapper<>();
//        queryWrapper.lambda().select(ProductInfo::getId,ProductInfo::getProductTypeId,ProductInfo::getDirection);
//        List<ProductInfo> infoList = productInfoMapper.selectList(queryWrapper);
//        Map<Long, ProductType> typeMap = productTypeService.selectProductTypeMap(null);
//        infoList.stream().map((info)->{
//            String name = typeMap.get(info.getProductTypeId()).getName();
//            info
//        })
        return productInfoMapper.selectProductDict(productInfo);
    }

    /**
     * 查询电子目录
     *
     * @param eCatalogVo
     * @return 产品信息
     */
    @Override
    public List<ECatalogInfoVo> getECatalog(ECatalogVo eCatalogVo) {
        QueryWrapper<ECatalogVo> queryWrapper = new QueryWrapper<>();
        queryWrapper
                .eq(StringUtils.isNotEmpty(eCatalogVo.getBrand()),"car_info.brand",eCatalogVo.getBrand())
                .eq(StringUtils.isNotEmpty(eCatalogVo.getVin()),"car_info.vin",eCatalogVo.getVin())
                .like(StringUtils.isNotEmpty(eCatalogVo.getNumber()),"product_info.number",eCatalogVo.getNumber())
                .like(StringUtils.isNotEmpty(eCatalogVo.getBrandCode()),"product_info.brand_code",eCatalogVo.getBrandCode());
        queryWrapper
                .eq(eCatalogVo.getSuperTypeId() != null,"product_type.parent_id",eCatalogVo.getSuperTypeId())
                .eq(eCatalogVo.getProductTypeId() != null,"product_type.id",eCatalogVo.getProductTypeId())
                .eq(StringUtils.isNotEmpty(eCatalogVo.getCarYear()),"car_info.car_year",eCatalogVo.getCarYear())
                .eq(StringUtils.isNotEmpty(eCatalogVo.getType()),"car_info.type",eCatalogVo.getType())
                .eq(StringUtils.isNotEmpty(eCatalogVo.getDisplacement()),"car_info.displacement",eCatalogVo.getDisplacement());

        return productInfoMapper.getECatalog(queryWrapper);
    }

    /**
     * 查询电子目录一级产品类型dict
     *
     * @param eCatalogVo
     * @return 汽车信息
     */
    @Override
    public List<ProductType> selectSuperClassDict(ECatalogVo eCatalogVo)
    {
        List<ECatalogInfoVo> eCatalog = this.getECatalog(eCatalogVo);
        List<Long> parentIdList = eCatalog.stream()
                .map(log -> log.getParentId())
                .distinct()
                .collect(Collectors.toList());
        if(CollectionUtil.isEmpty(parentIdList)){
            return new ArrayList<>();
        }
        return productTypeService.selectTypeListByIds(parentIdList);
    }

    /**
     * 查询电子目录二级产品类型dict
     *
     * @param eCatalogVo
     * @return 汽车信息
     */
    @Override
    public List<ProductType> selectSecondClassDict(ECatalogVo eCatalogVo)
    {
        List<ECatalogInfoVo> eCatalog = this.getECatalog(eCatalogVo);
        List<Long> idList = eCatalog.stream()
                .map(log -> log.getProductTypeId())
                .distinct()
                .collect(Collectors.toList());
        if(CollectionUtil.isEmpty(idList)){
            return new ArrayList<>();
        }
        return productTypeService.selectTypeListByIds(idList);
    }

    /**
     * 查询电子目录年份dict
     *
     * @param eCatalogVo
     * @return String
     */
    @Override
    public List<String> selectYearDict(ECatalogVo eCatalogVo)
    {
        List<ECatalogInfoVo> eCatalog = this.getECatalog(eCatalogVo);
        List<String> stringList = eCatalog.stream()
                .map(log -> log.getProductYear())
                .distinct()
                .collect(Collectors.toList());
        if(CollectionUtil.isEmpty(stringList)){
            return new ArrayList<>();
        }
        return stringList;
    }



    /**
     * convert查询电子目录
     *
     * @param eCatalogInfoVo
     * @return 产品信息
     */
    @Override
    public List<ECatalogInfoListVo> convertECatalog(List<ECatalogInfoVo> eCatalogInfoVo) {
        Map<Long, String> superTypeMap = productTypeService.selectSuperProductTypeMap(null);

        List<ECatalogInfoListVo> infoListVos = new ArrayList<>();

        Map<String, List<ECatalogInfoVo>> infoVoList = eCatalogInfoVo.stream()
                .collect(Collectors.groupingBy(
                        ECatalogInfoVo::getCategory,
                        Collectors.mapping(Function.identity(), Collectors.toList())
                ));

        infoVoList.entrySet().forEach(entry ->{
            ECatalogInfoListVo vo = new ECatalogInfoListVo();
            vo.setCategory(entry.getKey());
            vo.setInfo(entry.getValue());
            Long parentId = entry.getValue().get(0).getParentId();
            vo.setSuperClass(superTypeMap.get(parentId));
            infoListVos.add(vo);
        });
        return infoListVos;
    }


    /**
     * 查询电子目录Dict
     *
     * @param eCatalogVo
     * @return 产品信息
     */
    @Override
    public Map<String, Object> getECatalogDict(ECatalogVo eCatalogVo) {
        HashMap<String, Object> map = new HashMap<>();
        if(StringUtils.isNotEmpty(eCatalogVo.getBrand())){
            //车型
            List<String> types = carInfoService.selectCarTypeDict(eCatalogVo);
            //排量
            List<String> displacements = carInfoService.selectCarDisplacementDict(eCatalogVo);
            map.put("types",types);
            map.put("displacements",displacements);
        }
        //一级产品类型
        List<ProductType> superTypes = this.selectSuperClassDict(eCatalogVo);
        //二级产品类型
        List<ProductType> secondTypes = this.selectSecondClassDict(eCatalogVo);
        //生产年份
        List<String> years = this.selectYearDict(eCatalogVo);

        map.put("superTypes",superTypes);
        map.put("secondTypes",secondTypes);
        map.put("years",years);
        return map;
    }

    /**
     * 新增产品信息
     *
     * @param productInfo 产品信息
     * @return 结果
     */
    @Override
    public int insertProductInfo(ProductInfo productInfo)
    {
        productInfo.setCreateTime(DateUtils.getNowDate());
        return productInfoMapper.insertProductInfo(productInfo);
    }

    /**
     * 修改产品信息
     *
     * @param productInfo 产品信息
     * @return 结果
     */
    @Override
    public int updateProductInfo(ProductInfo productInfo)
    {
        productInfo.setUpdateTime(DateUtils.getNowDate());
        return productInfoMapper.updateProductInfo(productInfo);
    }

    /**
     * 批量删除产品信息
     *
     * @param ids 需要删除的产品信息主键
     * @return 结果
     */
    @Override
    public int deleteProductInfoByIds(Long[] ids)
    {
        return productInfoMapper.deleteProductInfoByIds(ids);
    }

    /**
     * 批量删除产品以及对应中间表数据信息
     *
     * @param ids 需要删除的产品信息主键
     * @return 结果
     */
    @Override
    @Transactional
    public int deleteProductByIds(Long[] ids){
        int delete = productInfoMapper.deleteProductInfoByIds(ids);
        if(delete <= 0){
            new RuntimeException("删除失败!");
        }
        Boolean productIds = productCarService.deleteByProductIds(ids);
        if (!productIds) {
            new RuntimeException("删除失败!");
        }
        return delete;
    }

    /**
     * 删除产品信息信息
     *
     * @param id 产品信息主键
     * @return 结果
     */
    @Override
    public int deleteProductInfoById(Long id)
    {
        return productInfoMapper.deleteProductInfoById(id);
    }
}
